//
//  VisitorView.swift
//  SwiftSina
//
//  Created by 七 on 2017/4/17.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit
import SnapKit

//MARK:-定义代理
@objc protocol VisitorViewDelegaate {
    //登录回调
    func loginBtnClick()
    //注册回调
    func registerBtnClick()
}
class VisitorView: UIView {

    //定义一个属性保存代理
    //一定要加上weak 避免循环引用
    weak var delegate: VisitorViewDelegaate?
    
    func setupVisitorViewInfo(isHome: Bool, imageName: String, message: String) {
        iconView?.isHidden = !isHome
        homeIconView?.image = UIImage(named: imageName)
        messageLabel?.text = message
        if isHome {
            startAnimate()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加子空间
        addSubview(maskBgView!)
        addSubview(iconView!)
        addSubview(homeIconView!)
        addSubview(messageLabel!)
        addSubview(loginBtn!)
        addSubview(registerBtn!)
        setupSubviewsFrame()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK:- 懒加载控件
    lazy var iconView: UIImageView? = {
        let iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iconView
    }()
    
    lazy var maskBgView: UIImageView? = {
        let mask = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        mask.contentMode = .scaleAspectFill
        return mask
    }()
    
    lazy var homeIconView: UIImageView? = {
        let homeIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return homeIconView
    }()
    
    lazy var messageLabel: UILabel? = {
       let messageLabel = UILabel()
        messageLabel.text = "哈空间登录奇偶发来"
        messageLabel.numberOfLines = 0
        messageLabel.textColor = .darkGray
        return messageLabel
    }()
    
    lazy var loginBtn: UIButton? = {
       let logoinBtn = UIButton()
        logoinBtn.setTitle("登录", for: .normal)
        logoinBtn.setTitleColor( .darkGray, for: .normal)
        logoinBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        logoinBtn.addTarget(self, action: #selector(loginClick), for: .touchUpInside)
        return logoinBtn
    }()
    
    lazy var registerBtn: UIButton? = {
        let registerBtn = UIButton()
        registerBtn.setTitle("注册", for: .normal)
        registerBtn.setTitleColor( .orange, for: .normal)
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        registerBtn.addTarget(self, action: #selector(registerClick), for: .touchUpInside)
        return registerBtn
    }()
    
    //MARK:- 布局子控件
    fileprivate func setupSubviewsFrame() {
        
        //设置背景
        iconView?.snp.makeConstraints({ (make) in
            make.center.equalToSuperview()
        })
        maskBgView?.snp.makeConstraints({ (make) in
            make.center.equalToSuperview()
            make.size.equalToSuperview()
        })
        homeIconView?.snp.makeConstraints({ (make) in
            make.center.equalToSuperview()
        })
        messageLabel?.snp.makeConstraints({ (make) in
            make.top.equalTo(iconView!.snp.bottom)
            make.centerX.equalTo(iconView!.snp.centerX)
        })
        registerBtn?.snp.makeConstraints({ (make) in
            make.left.equalTo(messageLabel!)
            make.top.equalTo(messageLabel!.snp.bottom).offset(10)
            make.width.equalTo(60)
            make.height.equalTo(35)
        })
        loginBtn?.snp.makeConstraints({ (make) in
            make.right.equalTo(messageLabel!.snp.right)
            make.top.equalTo(registerBtn!)
            make.width.equalTo(60)
            make.height.equalTo(35)
        })
        
    }
    
    func startAnimate() {
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        animate.toValue = 2*Double.pi
        animate.duration = 20
        animate.repeatCount = MAXFLOAT
        animate.isRemovedOnCompletion = false
        iconView?.layer.add(animate, forKey: nil)
    }
}
//MARK:- 点击事件
extension VisitorView {
    @objc func loginClick() {
        delegate?.loginBtnClick()
    }
    @objc func registerClick() {
        delegate?.registerBtnClick()
    }
}
